package com.kongming.kmdm.ws.rest;

import java.util.concurrent.CountDownLatch;

import org.glassfish.tyrus.server.Server;

import com.kongming.kmdm.ws.rest.config.WSConfig;
import com.kongming.kmdm.ws.rest.endpoint.WebSocketServerEndpoint;

public class WebsocketServer {

    private static CountDownLatch latch;

    public static void main (String [] args) {

        //read config
        WSConfig config = new WSConfig ();
        latch = new CountDownLatch (1);
        String [] host = config.getHostName ().split ("/");
        String path = ((host.length < 2) ? "" : "/" + host [1]) + "/ws";

        System.out.println (host [0] + "**********" + path);
        Server server = new Server (host [0], config.getPort (), path, null, WebSocketServerEndpoint.class);
        try {
            //start server
            server.start ();
            System.out.println ("websocket server start!");
            latch.await ();
        }
        catch (Exception e) {
            throw new RuntimeException (e);
        }
        finally {
            server.stop ();
        }
    }
}
